package org.jeecg.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.oms.entity.ProductSalesOrderDetail;
import org.jeecg.modules.oms.mapper.ProductSalesOrderDetailMapper;
import org.jeecg.modules.oms.service.ICfWindowDataService;
import org.jeecg.modules.oms.service.IProductSalesOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Comparator;

/**
 * 成品销售订单明细服务实现类
 */
@Slf4j
@Service
public class ProductSalesOrderDetailServiceImpl extends ServiceImpl<ProductSalesOrderDetailMapper, ProductSalesOrderDetail> implements IProductSalesOrderDetailService {

    @Autowired
    private ICfWindowDataService cfWindowDataService;

    @Override
    public List<ProductSalesOrderDetail> getDetailListByOrderId(String orderId) {
        List<ProductSalesOrderDetail> detailList = baseMapper.getDetailListByOrderId(orderId);
        // 按照窗号排序
        detailList.sort(Comparator.comparing(ProductSalesOrderDetail::getWindowNo, 
            Comparator.nullsLast(Comparator.naturalOrder())));
        return detailList;
    }

    @Override
    public List<ProductSalesOrderDetail> getDetailListByOrderIds(String orderIds) {
        List<ProductSalesOrderDetail> detailList = baseMapper.selectList(new QueryWrapper<ProductSalesOrderDetail>()
                .in("order_id",Arrays.asList(orderIds.split(",")))
                .orderByAsc("window_no"));
        return detailList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDetailByOrderId(String orderId) {
        // 获取订单下的所有明细
        List<ProductSalesOrderDetail> details = getDetailListByOrderId(orderId);
        
        // 删除每个明细对应的长风窗型确认文件数据
        for (ProductSalesOrderDetail detail : details) {
            cfWindowDataService.deleteAllByDetailId(detail.getId());
        }
        
        // 删除订单明细
        return baseMapper.deleteDetailByOrderId(orderId) >= 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrderDetailBatch(String orderId, List<ProductSalesOrderDetail> detailList) {
        if (detailList == null || detailList.isEmpty()) {
            return true;
        }

        List<ProductSalesOrderDetail> saveList = new ArrayList<>();
        for (ProductSalesOrderDetail detail : detailList) {
            detail.setOrderId(orderId);
            this.save( detail);
        }
        
        // 保存成功后，处理长风窗型确认文件数据
        for (ProductSalesOrderDetail detail : saveList) {
            try {
                cfWindowDataService.saveAllWindowData(detail);
            } catch (Exception e) {
                log.error("保存长风窗型确认文件数据失败，明细ID: {}", detail.getId(), e);
                throw new RuntimeException("保存长风窗型确认文件数据失败: " + e.getMessage());
            }
        }
        
        return true;
    }
} 